/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.persistence.datascope.DataScope;
import com.shanhesoft.java.common.persistence.service.impl.DataServiceImpl;
import com.shanhesoft.java.modules.exam.domain.Paper;
import com.shanhesoft.java.modules.exam.domain.PaperQue;
import com.shanhesoft.java.modules.exam.domain.dto.PaperDto;
import com.shanhesoft.java.modules.exam.domain.dto.PaperQueQueryCriteria;
import com.shanhesoft.java.modules.exam.domain.dto.PaperQueryCriteria;
import com.shanhesoft.java.modules.exam.domain.vo.PaperVo;
import com.shanhesoft.java.modules.exam.repository.PaperRepository;
import com.shanhesoft.java.modules.exam.service.PaperQueService;
import com.shanhesoft.java.modules.exam.service.PaperService;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 试卷管理ServiceImpl 试卷管理
 * @author admin
 * @version 2020-09-26 16:09:29
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class PaperServiceImpl extends DataServiceImpl<PaperRepository, Paper, PaperDto, String> implements PaperService{
	private final PaperQueService paperQueService;

	@Override
	public Map<String, Object> getCountInfo() {
		Map<String, Object> map = new HashMap<String, Object>();
		int userCount = baseMapper.findUserCount();
		int paperCount = baseMapper.findPaperCount();
		int queCount = baseMapper.findQueCount();
		int courseCount = baseMapper.findCourseCount();

		map.put("userCount", userCount);
		map.put("paperCount", paperCount);
		map.put("queCount", queCount);
		map.put("courseCount", courseCount);

		return map;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdate(PaperDto paperDto) {
		super.saveOrUpdate(paperDto);

		// 先删除，再写入
		PaperQueQueryCriteria paperQueQueryCriteria = new PaperQueQueryCriteria();
		paperQueQueryCriteria.setPaperId(paperDto.getId());
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(paperQueQueryCriteria);
		paperQueService.remove(wrapper);

		List<PaperQue> paperQueList = new ArrayList<PaperQue>();
		getPageQueList(paperQueList, paperDto.getSingleList(), paperDto.getId(), 0L);
		getPageQueList(paperQueList, paperDto.getMultipleList(), paperDto.getId(), 1L);
		getPageQueList(paperQueList, paperDto.getJudgeList(), paperDto.getId(), 2L);
		getPageQueList(paperQueList, paperDto.getFillList(), paperDto.getId(), 3L);
		getPageQueList(paperQueList, paperDto.getProblemsList(), paperDto.getId(), 4L);

		paperQueService.saveOrUpdateBatch(paperQueList);
	}

	private void getPageQueList(List<PaperQue> paperQueList, List<Map<String, Object>> queList, String paperId, long queType) {
		if(null != queList) {
			for(Map<String, Object> map : queList) {
				PaperQue paperQue = new PaperQue();
				paperQue.setPaperId(paperId);
				paperQue.setQueId((String)map.get("id"));
				paperQue.setScore(map.get("score") == null ? 0: (Integer)map.get("score"));
				paperQue.setQueType(queType);

				paperQueList.add(paperQue);
			}
		}
	}

	/**
	 * 分页查询试卷信息
	 *
	 * @param pm 分页对象
	 * @return
	 */
	@Override
	public IPage<PaperVo> findPage(PageModel pm, PaperQueryCriteria paperQueryCriteria) {
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, paperQueryCriteria);
//		wrapper.like("a.name", Paper.F_NAME);
		wrapper.eq("a.del_flag", Paper.FLAG_NORMAL);
//		wrapper.eq("b.del_flag", Paper.FLAG_NORMAL);

		if("created_date".equals(paperQueryCriteria.getSort())) {
			wrapper.orderByDesc("a.created_date");
		} else if("participate_num".equals(paperQueryCriteria.getSort())) {
			wrapper.orderByDesc("a.participate_num");
		}

		if(StringUtils.isNotEmpty(paperQueryCriteria.getType())) {
			if(paperQueryCriteria.getType().indexOf(",") > -1) {
				wrapper.in("a.type", paperQueryCriteria.getType().split(","));
			} else {
				wrapper.eq("a.type", paperQueryCriteria.getType());
			}
		}


		IPage<PaperVo> paperVoIPage = repository.findPaperVoPage(pm, wrapper);
		return paperVoIPage;
	}

	/**
	 * 通过ID查询试卷信息
	 *
	 * @param id 用户ID
	 * @return 用户信息
	 */
	@Override
//	@Cacheable(key = "'findPaperVoById:' + #p0")
	public PaperVo findUserVoById(String id) {
		PaperVo userVo = baseMapper.findPaperVoById(id);
		return userVo;
	}
}
